<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addRole(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <!-- {{ row }} -->
              <el-button v-if="row.type === 1" size="small" type="text" @click="addRole(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="editRole(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog :visible="showDialog" :title="title" @close="closeDialog">
          <el-form v-loading="loading" label-width="100px">
            <el-form-item label="权限名称">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="权限标识">
              <el-input v-model="formData.code" />
            </el-form-item>
            <el-form-item label="权限描述">
              <el-input v-model="formData.description" />
            </el-form-item>
            <el-form-item label="权限启用">
              <el-switch
                v-model="formData.enVisible"
                active-text="启用"
                active-value="1"
                inactive-text="不启用"
                inactive-value="0"
              />
            </el-form-item>
          </el-form>

          <template #footer>
            <div style="text-align: right;">
              <el-button @click="closeDialog">取消</el-button>
              <el-button type="primary" @click="clickSubmit">确定</el-button>
            </div>
          </template>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import {
  reqDelPermission,
  reqGetPermissionList,
  reqAddPermission,
  reqGetPermissionDetail,
  reqUpdatePermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Permissions',
  data() {
    return {
      list: [], // 所有权限列表数据
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      loading: false,
      title: '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取所有权限列表
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      this.list = tranListToTreeData(data, '0')
    },
    // 删除权限
    delRole(id) {
      // 1. 确认消息提示
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        type: 'warning'
      }).then(async() => {
        // 2. 携带参数发送请求
        const { message } = await reqDelPermission(id)
        // 3.提示用户
        this.$message.success(message)
        // 4. 重新渲染
        this.getPermissionList()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 展示添加对话框
    addRole(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
      this.title = '新增权限'
    },
    // 关闭对话框
    closeDialog() {
      this.showDialog = false
      // 关闭对话框重置表单
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 点击确定按钮完成新增/编辑
    async clickSubmit() {
      // 1. 预校验
      if (!this.formData.name || !this.formData.code) return this.$message.warning('请输入权限名称或权限标识')
      if (this.formData.id) {
        // 编辑
        const { message } = await reqUpdatePermission(this.formData)
        // 3. 提示用户
        this.$message.success(message)
      } else {
        // 新增
        // 2. 发送请求
        const { message } = await reqAddPermission(this.formData)
        // 3. 提示用户
        this.$message.success(message)
      }
      // 4. 关闭对话框
      this.closeDialog()
      // 5. 重新渲染
      this.getPermissionList()
    },
    // 点击查看按钮查询权限点信息
    async editRole(id) {
      this.loading = true
      this.title = '编辑权限'
      // 1. 展示对话框
      this.showDialog = true
      // 2. 发送请求查询权限点信息
      const { data } = await reqGetPermissionDetail(id)
      //  3.将查询结果赋值给formData
      this.formData = data
      this.loading = false
    }
  }
}
</script>
